{% macro mark_active_route(route_name) %}
    {{ dict(class='active')|xmlattr if request.matched_route.name == route_name }}
{% endmacro %}


{% macro pagination(collection) %}
    {% if collection.first_page != collection.last_page %}
        <ul class="pager">
            {% if collection.page != collection.first_page %}
                <li class="previous">
                    {{ collection.pager('$link_previous', symbol_previous='Previous') }}
				</li>
            {% endif %}
            {% if collection.page != collection.last_page %}
                <li class="next">
                    {{ collection.pager('$link_next', symbol_next='Next') }}
                </li>
            {% endif %}
        </ul>
    {% endif %}    
{% endmacro %}


{% macro form_field(field) %}
    {% set error = field.errors and field.errors.pop() or None %}
    <div class="control-group {{ error and 'error' }}">
        {{ field.label(class='control-label') }}
        <div class="controls">
            {{ field(**kwargs)|safe }}
            {% if error %}
                <span class="help-inline">{{ error }}</span>
            {% endif %}
        </div>
    </div>
{% endmacro %}


{% macro add_company_form(form) %}
    <form method="post" class="form-horizontal span12">
        {{ form_field(form.symbol, placeholder='GOOG, AMZN, AAPL, etc.', autofocus='autofocus') }}
        <div class="control-group">
            <div class="controls">
                <span class="help-block">Separate symbols by a comma and/or a new line.</span>
                <input type="submit" name="add_companies" value="Add" class="btn" />
            </div>
        </div>
    </form>
{% endmacro %}


{% macro company_stream(companies) %}
    <table class="table table-bordered table-striped">
        <tr>
            <th>Symbol</th>
            <th>Name</th>
            <th>Exchange</th>
        </tr>
        {% for company in companies %}
            <tr>
                <td><a href="{{ request.route_url('company.view', id=company.id) }}">{{ company.symbol }}</a></td>
                <td>{{ company.name }}</td>
                <td>{{ company.exchange }}</td>
            </tr>
        {% endfor %}
    </table>
{% endmacro %}


{% macro stock_quote_stream(stock_quotes) %}
    <table class="table table-bordered table-striped">
        <tr>
            <th>Date</th>
            <th>Open Price</th>
            <th>High Price</th>
            <th>Low Price</th>
            <th>Close Price</th>
            <th>Adjusted Close Price</th>
            <th>Volume</th>
        </tr>
        {% for stock_quote in stock_quotes %}
            <tr>
                <td>{{ stock_quote.date|datetimeformat }}</td>
                <td>{{ stock_quote.open_price }}</td>
                <td>{{ stock_quote.high_price }}</td>
                <td>{{ stock_quote.low_price }}</td>
                <td>{{ stock_quote.close_price }}</td>
                <td>{{ stock_quote.adjusted_close_price }}</td>
                <td>{{ stock_quote.volume }}</td>
            </tr>
        {% endfor %}
    </table>
{% endmacro %}


{% macro company_info(company, prediction, stock_quotes, news) %}
    <div class="well">
        <div class="clearfix">
	        <h4 class="pull-left" title="{{ company.exchange }}">
	            <a href="{{ company.yahoo_finance_url }}" target="_blank">{{ company.name }}</a>
	            &ndash;
	            {{ company.symbol }}
	            &ndash;
	            <span class="muted">({{ company.current_price }})</span>
	        </h4>
	        <a class="pull-right btn remove-company" href="{{ request.route_url('company.remove', id=company.id) }}">remove company</a>
        </div>
        <div>
            <span title="Short Term" class="label {{ prediction[0] and 'label-success' or 'label-important' }}">Bayesian Prediction: {{ prediction[0] and 'Long' or 'Short' }}</span>
            &middot;
            <span title="Long Term" class="label {{ prediction[1] and 'label-success' or 'label-important' }}">SVM Prediction: {{ prediction[1] and 'Long' or 'Short' }}</span>
        </div>
    </div>
    <div class="well clearfix">
		<ul class="nav nav-tabs" id="info-tab">
            <li class="active"><a href="#quotes">{{ stock_quotes.item_count|pluralize('Quote', 'Quotes') }}</a></li>
		    <li><a href="#chart">Chart</a></li>
		    <li><a href="#news">News</a></li>
		</ul>
		<div class="tab-content">
            <div class="tab-pane active" id="quotes">
                {% if stock_quotes %}
                    {{ stock_quote_stream(stock_quotes) }}
                    {{ pagination(stock_quotes) }}
                {% else %}
                    <p class="alert">No stock quotes in database for this company yet. Give it some time.</p>
                {% endif %}
            </div>
		    <div class="tab-pane" id="chart">
                <a href="{{ company.chart }}" target="_blank"><img src="{{ company.chart }}" /></a>
		    </div>
		    <div class="tab-pane" id="news">
                <h5>{{ news.feed.title }}</h5>
	            <ul>
	            {% for entry in news.entries if entry.description %}
	               <li>
	                   <a href="{{ entry.link }}" target="_blank">{{ entry.title }}</a>
	                   <small class="muted">&ndash; {{ entry.description|truncate(300) }}</small>
	               </li>
                {% else %}
                    <li>No news to show.</li>
	            {% endfor %}
	            </ul>
		    </div>
		</div>
    </div>
{% endmacro %}
